<?php
/**
 * User: hufeng
 * Date: 2017/10/11
 * Desc: 交易管理
 */

namespace Admin\Controller;
use Model\AdminCustomerModel;
use Model\StockModel;
use Model\TradeDealhistoryModel;
use Model\TradeUserAssetModel;
use Model\UserModel;

class TradeController extends BaseController{

    //交易列表
    public function index(){
        if($_POST['__hash__'] || !$_GET['p']){
            session('searchParam',null);
        }
        $sessionParam = session('searchParam');
        $sessionArr['uinfo']  = @$_POST['uinfo'] ? $_POST['uinfo'] : @$sessionParam['uinfo'];
        $sessionArr['stockno']  = @$_POST['stockno'] ? $_POST['stockno'] : @$sessionParam['stockno'];
        $sessionArr['stockname']  = @$_POST['stockname'] ? $_POST['stockname'] : @$sessionParam['stockname'];
        $sessionArr['start']  = @$_POST['start'] ? $_POST['start'] : @$sessionParam['start'];
        $sessionArr['end']  = @$_POST['end'] ? $_POST['end'] : @$sessionParam['end'];
        session('searchParam',$sessionArr);
        $page = @$_GET['p'] ? $_GET['p'] : 1;

        $where = [];
        if($sessionArr['uinfo']){
            $uModel = new UserModel();
            $uwhere['_string'] = "tel='{$sessionArr['name']}' or real_name='{$sessionArr['name']}'";
            $uRes = $uModel->findData($uwhere,'Id');
            $uid = $uRes['id'] ? $uRes['id'] : 0;
            $where['_string'] = "buy_uid='{$uid}' or sale_uid='{$uid}'";
        }
        if($sessionArr['stockno']){
            $sModel = new StockModel();
            $swhere['stock_no'] = $sessionArr['stockno'];
            $sRes = $sModel->findData($swhere);
            $sid = $sRes['id'] ? $sRes['id'] : 0;
            $where['sid'] = $sid;
        }
        if($sessionArr['stockname']){
            $sModel = new StockModel();
            $swhere['stock_name'] = array('like','%'.$sessionArr['stockname'].'%');
            $sRes = $sModel->findData($where);
            $sid = $sRes['id'] ? $sRes['id'] : 0;
            $where['sid'] = $sid;
        }
        if($sessionArr['start'] && $sessionArr['end']){
            $where['_string'] = "addtime>='{$sessionArr['start']}' and addtime<='{$sessionArr['end']} 23:59:59'";
        }
        $tdModel = new TradeDealhistoryModel();
        $resList = $tdModel->search($where,$page,$this->pernum);
        $uidArr = $sidArr = [];
        foreach ($resList as $val){
            $uidArr[] = $val['buy_uid'];
            $uidArr[] = $val['sale_uid'];
            $sidArr[] = $val['sid'];
        }
        $uRes = [];
        $sRes = [];
        if($sidArr){
            $uModel = new UserModel();
            $uList = $uModel->selectData(['id'=>['in',$uidArr]]);
            foreach ($uList as $val){
                $uRes[$val['id']]['tel'] = $val['tel'] ? $val['tel'] : $val['nick_name'];
                $uRes[$val['id']]['rname'] = $val['real_name'];
            }
            $sModel = new StockModel();
            $sList = $sModel->selectData(['id'=>['in',$sidArr]]);
            foreach ($sList as $val){
                $sRes[$val['id']]['sno'] = $val['stock_no'];
                $sRes[$val['id']]['sname'] = $val['stock_name'];
            }
        }

        $totalNum = $tdModel->getTotalNum($where);
        $pageShow = getPage($totalNum,$this->pernum);
        //所有分类
        $this->assign('pageShow',$pageShow);
        $this->assign('page',$page);
        $this->assign('resList',$resList);
        $this->assign('totalNum',$totalNum);
        $this->assign('postArr',$sessionArr);
        $this->assign('uRes',$uRes);
        $this->assign('sRes',$sRes);
        $this->display('trade/index');
    }
    //持仓列表
    public function barn(){
        if($_POST['__hash__'] || !$_GET['p']){
            session('searchParam',null);
        }
        $sessionParam = session('searchParam');
        $sessionArr['uinfo']  = @$_POST['uinfo'] ? $_POST['uinfo'] : @$sessionParam['uinfo'];
        $sessionArr['stockno']  = @$_POST['stockno'] ? $_POST['stockno'] : @$sessionParam['stockno'];
        $sessionArr['stockname']  = @$_POST['stockname'] ? $_POST['stockname'] : @$sessionParam['stockname'];
        session('searchParam',$sessionArr);
        $page = @$_GET['p'] ? $_GET['p'] : 1;

        $where = [];
        $where['status'] = array('neq',3);
        if($sessionArr['uinfo']){
            $uModel = new UserModel();
            $uwhere['_string'] = "tel='{$sessionArr['name']}' or real_name='{$sessionArr['name']}'";
            $uRes = $uModel->findData($uwhere,'Id');
            $uid = $uRes['id'] ? $uRes['id'] : 0;
            $where['uid'] = $uid;
        }
        if($sessionArr['stockno']){
            $sModel = new StockModel();
            $swhere['stock_no'] = $sessionArr['stockno'];
            $sRes = $sModel->findData($where);
            $sid = $sRes['id'] ? $sRes['id'] : 0;
            $where['sid'] = $sid;
        }
        if($sessionArr['stockname']){
            $sModel = new StockModel();
            $swhere['stock_name'] = array('like','%'.$sessionArr['stockname'].'%');
            $sRes = $sModel->findData($where);
            $sid = $sRes['id'] ? $sRes['id'] : 0;
            $where['sid'] = $sid;
        }
        $tuaModel = new TradeUserAssetModel();
        $resList = $tuaModel->search($where,$page,$this->pernum);
        $uidArr = $sidArr = [];
        foreach ($resList as $val){
            $uidArr[] = $val['uid'];
            $sidArr[] = $val['sid'];
        }
        $uRes = [];
        $sRes = [];
        $tdRes = [];
        if($sidArr){
            $uModel = new UserModel();
            $uList = $uModel->selectData(['id'=>['in',$uidArr]]);
            foreach ($uList as $val){
                $uRes[$val['id']]['tel'] = $val['tel']?$val['tel']:$val['nick_name'];
                $uRes[$val['id']]['rname'] = $val['real_name']?$val['real_name']:'--';
            }
            $sModel = new StockModel();
            $sList = $sModel->selectData(['id'=>['in',$sidArr]]);
            foreach ($sList as $val){
                $sRes[$val['id']]['sno'] = $val['stock_no'];
                $sRes[$val['id']]['sname'] = $val['stock_name'];
            }
            //股票当前价格
            $tdModel = new TradeDealhistoryModel();
            $sidStr = implode(',',$sidArr);
            $tdList = $tdModel->query("SELECT MAX(id) id FROM `trade_dealhistory` WHERE sid IN ({$sidStr})  GROUP BY sid ORDER BY id DESC");
            $tdIdArr = [];
            foreach ($tdList as $val){
                $tdIdArr[] = $val['id'];
            }
            if($tdIdArr){
                $tdList = $tdModel->selectData(['id'=>['in',$tdIdArr]],'sid,avg_price');
                foreach ($tdList as $val){
                    $tdRes[$val['sid']] = $val['avg_price'];
                }
            }
        }

        $totalNum = $tuaModel->getTotalNum($where);
        $pageShow = getPage($totalNum,$this->pernum);
        //所有分类
        $this->assign('pageShow',$pageShow);
        $this->assign('page',$page);
        $this->assign('resList',$resList);
        $this->assign('totalNum',$totalNum);
        $this->assign('postArr',$sessionArr);
        $this->assign('uRes',$uRes);
        $this->assign('sRes',$sRes);
        $this->assign('tdRes',$tdRes);
        $this->display('trade/barn');
    }
    //股票买卖
    public function oprate(){
        $acModel = new AdminCustomerModel();
        $acRes = $acModel->findData(['id'=>$this->adminUserData['uid']]);
        $uModel = new UserModel();
        $uRes = $uModel->findData(['id'=>$acRes['uid']]);

        //所有股票
        $sModel = new StockModel();
        $sList = $sModel->selectData(['status'=>1],['id','stock_no','stock_name']);
        $sresArr = $snameArr = [];
        foreach ($sList as $val){
            $sname = $val['stock_no'].'-'.$val['stock_name'];
            $snameArr[] = $sname;
            $sresArr[$sname] = $val['id'];
        }
        $this->assign('snameArr',json_encode($snameArr,JSON_UNESCAPED_UNICODE));
        $this->assign('sresArr',json_encode($sresArr,JSON_UNESCAPED_UNICODE));
        $this->assign('timeNow',time()*1000);
        $this->assign('serverApi',SERVER_API_URL);
        $this->assign('openId',$uRes['open_id']);
        $this->assign('socketUrl',SOCKET_API_URL);
        $this->display('trade/oprate');
    }
}